package com.coffeeloversupportlibrary.coffeelover.mercury.login;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatButton;
import androidx.fragment.app.Fragment;


import com.coffeeloversupportlibrary.R;
import com.coffeeloversupportlibrary.coffeelover.mercury.utils.HttpUtils;
import com.google.gson.Gson;

import org.jetbrains.annotations.NotNull;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * @author 盛守义
 * @createTime 2021/10/24 15:43
 * @projectName TurningCoffee
 * @className ForgetPwdActivity.java
 * @description TODO
 */
public class ForgetPwdActivity extends AppCompatActivity {
    private View view;
    private Activity activity = ForgetPwdActivity.this;

    private EditText et_mail, et_verifyfgtpwd;
    private AppCompatButton btn_verifyfgtpwd, btn_submitfgtpwd;
    private String email, confirmCode;
    private String Verification_Code;



    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forgetpwd);
        initViews();
        setListener();
    }


    class ForgetPwdOnClickListener implements View.OnClickListener{

        @Override
        public void onClick(View view) {
            switch (view.getId()){
                case R.id.btn_verifyfgtpwd:
                    email = et_mail.getText().toString().trim();
                    if(email == null|| email.equals("")){
                        Toast.makeText(activity, "邮箱不能为空", Toast.LENGTH_SHORT).show();
                    }else{
                        Gson gson = new Gson();

                        JSONObject jsonObject = new JSONObject();
                        try {
                            jsonObject.put("emailAddress", email);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                        String jsonString = jsonObject.toString();

                        HttpUtils httpUtils = new HttpUtils();

                        FormBody.Builder requestBuild = new FormBody.Builder();
                        RequestBody requestBody = requestBuild.add("emailAddress", et_mail.getText().toString().trim()).build();

                        //url
                        Call call = httpUtils.getCall(requestBody, "/email");
                        call.enqueue(new Callback() {
                            @Override
                            public void onFailure(@NotNull Call call, @NotNull IOException e) {
                                ForgetPwdActivity.this.runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        Toast.makeText(ForgetPwdActivity.this, "网络连接崩溃了，绝对绝对不是我们的错", Toast.LENGTH_SHORT).show();
                                    }
                                });
                            }

                            @Override
                            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                                String res = response.body().string();
                                Log.e("forget验证码", res);
                                ForgetPwdActivity.this.runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        Verification_Code = res;
                                        if(Verification_Code.equals("邮件发送失败")){
                                            Toast.makeText(ForgetPwdActivity.this, Verification_Code, Toast.LENGTH_SHORT).show();
                                        }
                                    }
                                });
                            }
                        });
                    }
                    break;
                case R.id.btn_submitfgtpwd:
                    email = et_mail.getText().toString().trim();
                    confirmCode = et_verifyfgtpwd.getText().toString().trim();
                    if(confirmCode == null|| confirmCode.equals("")){
                        Toast.makeText(activity, "请输入验证码", Toast.LENGTH_SHORT).show();
                    }else if(confirmCode == Verification_Code || confirmCode.equals(Verification_Code)){
                        //跳转
                        Intent intent = new Intent(activity, ResetPasswordActivity.class);
                        intent.putExtra("usermail", email);
                        finish();
                    }
                    break;
            }
        }
    }


    void initViews(){
        et_mail = findViewById(R.id.et_mailfgtpwd);
        et_verifyfgtpwd = findViewById(R.id.et_verifyfgtpwd);
        btn_submitfgtpwd = findViewById(R.id.btn_submitfgtpwd);
        btn_verifyfgtpwd = findViewById(R.id.btn_verifyfgtpwd);
    }

    void setListener(){
        ForgetPwdOnClickListener listener = new ForgetPwdOnClickListener();
        btn_submitfgtpwd.setOnClickListener(listener);
        btn_verifyfgtpwd.setOnClickListener(listener);
    }
}
